package org.ielse.frame.test.fragment;

import org.ielse.frame.App;
import org.ielse.frame.BaseFragment;
import org.ielse.frame.R;
import org.ielse.frame.model.AppSettings;
import org.ielse.frame.t.view.markushi.ActionView;
import org.ielse.frame.t.view.markushi.action.Action;
import org.ielse.frame.t.view.markushi.action.BackAction;
import org.ielse.frame.t.view.markushi.action.DrawerAction;
import org.ielse.frame.test.MainActivity;
import org.ielse.frame.test.NormalActivity;
import org.ielse.frame.test.SplashActivity;
import org.ielse.frame.util.BaseUtil;
import org.ielse.frame.view.SwitchView;
import org.ielse.frame.view.SwitchView.OnStateChangedListener;
import org.ielse.frame.view.TitleLayout;

import com.nineoldandroids.view.ViewHelper;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class MainFragment extends BaseFragment {

	private MainActivity activity;

	@Override public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof MainActivity) {
			this.activity = (MainActivity) activity;
		}
	}

	private TitleLayout layTitle;

	private SwitchView viewSwitch1, viewSwitch3;

	private AppSettings appSettings;

	private ActionView viewAction;
	private final Action drawerAction = new DrawerAction();
	private final Action backAction = new BackAction();

	@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_main, null);
		layTitle = (TitleLayout) view.findViewById(R.id.lay_title);
		layTitle.setTextTitle(R.string.app_name);
		
		
		viewAction = new ActionView(activity);
		final int size = BaseUtil.dipToPx(42);
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(size,size);
		lp.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
		viewAction.setLayoutParams(lp);
		final int padding = BaseUtil.dipToPx(8);
		viewAction.setPadding(padding, padding, padding, padding);
		viewAction.setColor(0xffffffff);
		viewAction.setAction(drawerAction);
		
		viewAction.setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View view) {
				viewAction.setAction( backAction , ActionView.ROTATE_COUNTER_CLOCKWISE);
				activity.openRightMenu();
			}
		});
		layTitle.setRightView(viewAction);

		
		

		appSettings = App.i().getAppSettings();
		viewSwitch1 = (SwitchView) view.findViewById(R.id.view_switch1);
		viewSwitch1.setState(appSettings.isShinyTrace());

		viewSwitch1.setOnStateChangedListener(new OnStateChangedListener() {
			@Override public void toggleToOn() {
				viewSwitch1.toggleSwitch(true);
				appSettings.setShinyTrace(true);
				appSettings.update(appSettings.getId());
			}

			@Override public void toggleToOff() {
				viewSwitch1.toggleSwitch(false);
				appSettings.setShinyTrace(false);
				appSettings.update(appSettings.getId());
			}
		});

		viewSwitch3 = (SwitchView) view.findViewById(R.id.view_switch3);
		viewSwitch3.setState(false);
		viewSwitch3.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
			@Override public void toggleToOn() {
				viewSwitch3.postDelayed(new Runnable() {
					@Override public void run() {
						viewSwitch3.toggleSwitch(true);
					}
				}, 500);
			}

			@Override public void toggleToOff() {
				viewSwitch3.postDelayed(new Runnable() {
					@Override public void run() {
						viewSwitch3.toggleSwitch(false);
					}
				}, 300);
			}
		});
		
		ViewHelper.setAlpha(viewSwitch3, 0.5f);

		view.findViewById(R.id.btn_start_activity_b).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View arg0) {
				startActivity(NormalActivity.class);
			}
		});
		
		view.findViewById(R.id.btn_parallax).setOnClickListener(new View.OnClickListener() {
			@Override public void onClick(View arg0) {
				startActivity(SplashActivity.class);
			}
		});

		
		return view;
	}
	
	public void setActionViewAnimationProgress(float progress){
		viewAction.setAnimationProgress(progress);
	}
}
